package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.TaskListBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class SendTaskAdapter extends RecyclerView.Adapter<SendTaskAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<TaskListBean.PageBean.ListBean> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;
    private boolean isFinish = false;

    public SendTaskAdapter(Context context, boolean isFinish) {
        this.context = context;
        this.isFinish = isFinish;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_send_task, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            TaskListBean.PageBean.ListBean listBean = mData.get(position);
            String taskName = listBean.getName();
            String creatTime = listBean.getCreateTime();

            if (!StringUtils.isEmpty(taskName)) {
                holder.tvTitle.setText(taskName + "");
                holder.tvTime.setText("任务时间：" + creatTime + "");
            }
            if (isFinish) {
                holder.ivImg.setBackground(context.getDrawable(R.mipmap.icon_task2_finish));
            }else{
                holder.ivImg.setBackground(context.getDrawable(R.mipmap.icon_task2));
            }


        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void upData(List<TaskListBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View view;
        private final ImageView ivImg;
        private final TextView tvTime;
        private final TextView tvTaskType;
        private final TextView tvTitle;

        public ActivityHolder(View itemView) {
            super(itemView);
            view = itemView;
            ivImg = (ImageView) view.findViewById(R.id.iv_img);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvTaskType = (TextView) view.findViewById(R.id.tv_task_type);
        }
    }

}
